package com.epfresh.supplier.entity;


import android.util.Log;
import com.epfresh.api.utils.JsonUtils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MerChantInfo implements Serializable {

    private String subjectCode;// 主体码
    private String districtCode;
    private String address;
    private String certStatus;
    private String districtName;
    private Auth auth;
    private String contactName;

    public String getSubjectCode() {
        return subjectCode;
    }

    public void setSubjectCode(String subjectCode) {
        this.subjectCode = subjectCode;
    }

    private String provinceCode;
    private String cityCode;
    private String certId;
    private String type;
    private String storeId;
    private Object lastUsed;
    private String openedAreaCode;
    private String cityName;
    private String name;
    private Object createdTime;
    private String storeName;
    private String id;
    //0,未开,1,开店,2,过,3,未过,4,冻结
    private String storeStatus;
    private String provinceName;
    private Object shortName;
    private Object contactPhone;
    private boolean authNeedBind;

    private List<String> disabledResources;

    //员工信息
    private StaffInfo staffInfo;
    //是否是选中
    private boolean isSelect;

    public boolean isAuthNeedBind() {
        return authNeedBind;
    }

    public void setAuthNeedBind(boolean authNeedBind) {
        this.authNeedBind = authNeedBind;
    }

    public List<String> getDisabledResources() {
        return disabledResources;
    }

    public void setDisabledResources(List<String> disabledResources) {
        this.disabledResources = disabledResources;
    }

    public String getStoreStatus() {
        //如果开店状态是null默认为未开店
        return storeStatus == null ? "0" : storeStatus;
    }

    public void setStoreStatus(String storeStatus) {
        this.storeStatus = storeStatus;
    }

    public boolean isSelect() {
        return isSelect;
    }

    public void setSelect(boolean select) {
        isSelect = select;
    }

    public StaffInfo getStaffInfo() {
        return staffInfo;
    }

    public void setStaffInfo(StaffInfo staffInfo) {
        this.staffInfo = staffInfo;
    }

    public String getDistrictCode() {
        return districtCode;
    }

    public void setDistrictCode(String districtCode) {
        this.districtCode = districtCode;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCertStatus() {
        return certStatus;
    }

    public void setCertStatus(String certStatus) {
        this.certStatus = certStatus;
    }

    public String getDistrictName() {
        return districtName;
    }

    public void setDistrictName(String districtName) {
        this.districtName = districtName;
    }

    public Auth getAuth() {
        return auth;
    }

    public void setAuth(Auth auth) {
        this.auth = auth;
    }

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public String getProvinceCode() {
        return provinceCode;
    }

    public void setProvinceCode(String provinceCode) {
        this.provinceCode = provinceCode;
    }

    public String getCityCode() {
        return cityCode;
    }

    public void setCityCode(String cityCode) {
        this.cityCode = cityCode;
    }

    public String getCertId() {
        return certId;
    }

    public void setCertId(String certId) {
        this.certId = certId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getStoreId() {
        return storeId;
    }

    public void setStoreId(String storeId) {
        this.storeId = storeId;
    }

    public Object getLastUsed() {
        return lastUsed;
    }

    public void setLastUsed(Object lastUsed) {
        this.lastUsed = lastUsed;
    }

    public String getOpenedAreaCode() {
        return openedAreaCode;
    }

    public void setOpenedAreaCode(String openedAreaCode) {
        this.openedAreaCode = openedAreaCode;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getName() {
        return name;
    }

    public String getNamePlus() {

        String s = "";
        if (name == null) {
            name = "";
        }
        //0,未开,1,开店,2,过,3,未过,4,冻结
        //未开店
        if ("0".equals(storeStatus)) {
            s = name;
        } else if ("1".equals(storeStatus)) {
            s = storeName + " (审核中)";
        } else if ("3".equals(storeStatus)) {
            s = storeName;
        } else if ("4".equals(storeStatus)) {
            s = storeName + " (冻结)";
        } else {
            s = storeName;
        }
        return s;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Object createdTime) {
        this.createdTime = createdTime;
    }

    public String getStoreName() {
        return storeName == null ? "" : storeName;
    }

    public void setStoreName(String storeName) {
        this.storeName = storeName;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getProvinceName() {
        return provinceName;
    }

    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }

    public Object getShortName() {
        return shortName;
    }

    public void setShortName(Object shortName) {
        this.shortName = shortName;
    }

    public Object getContactPhone() {
        return contactPhone;
    }

    public void setContactPhone(Object contactPhone) {
        this.contactPhone = contactPhone;
    }

    public boolean isGroup() {
        if (auth != null) {
            return auth.isGroup();
        }
        return false;
    }

    public boolean isCategoryLevelOne() {
        if (auth != null) {
            return auth.isCategoryLevelOne();
        }
        return false;

    }

    public String getStoreCategoryType() {
        if (auth != null) {
            return auth.getStoreCategoryType();
        }
        return "";
    }

    public String getOperateName() {
        if (staffInfo != null) {
            String name = staffInfo.getName();
            return name == null ? "" : name;
        }
        return "";
    }

    public boolean isNeedBind() {
        if (auth != null) {
            return auth.isNeedBind();
        }
        return false;
    }

    public boolean isResources(String key) {
        if (staffInfo != null) {
            return staffInfo.isResources(key);
        }
        return false;
    }

    public Map<String, Object> getResources() {
        if (staffInfo != null) {
            return staffInfo.getResources();
        }

        return new HashMap<>();
    }

    public Boolean getResources(String key) {
        if (staffInfo != null) {
            return staffInfo.getResourcesItem(key);
        }

        return null;
    }

    /*"buy": 我要买,
     "sell": 我要卖*/
    public boolean isOuthBuyer() {
        if (auth != null) {
            return auth.isiBuy();// && isResources("root.app.supplier.buy");
        }
        return false;
    }

    public boolean isOuthSeller() {
        if (auth != null) {
            return auth.isiSell()/* && isResources("root.app.supplier.sell") */ || "0".equals(storeStatus);
        }
        return false;
    }


    public boolean isBuyer() {


        Boolean isBuyer = getResources("root.app.supplier.buy");
        return isBuyer == null ? false : isBuyer;
    }

    public boolean isOwned() {
        if (staffInfo != null) {
            return staffInfo.isOwned();
        }
        return false;
    }


    //收款"root.app.pos.write_off" : true,
    public boolean isWriteOff() {
        return isResources("root.app.supplier.standing_book") && "2".equals(storeStatus);
    }

    //联营备货
    public boolean isJoinPrepare() {
        return isResources("root.app.supplier.joint.order") && "2".equals(storeStatus);
    }

    //联营报价
    public boolean isJoinPrice() {
        return isResources("root.app.supplier.joint.product") && "2".equals(storeStatus);
    }


    //变价/进货
    public boolean isEditPrice() {

         /* 我要买    root.app.supplier.buy
            店铺装修    root.app.supplier.store.decoration
            店铺管理    root.app.supplier.store
            消息推送    root.app.supplier.push_message
            员工管理    root.app.supplier.employee
            客户管理    root.app.supplier.customer
            我的收入    root.app.supplier.income
            我的台帐    root.app.supplier.standing_book
            评价       root.app.supplier.comment
            优惠券     root.app.supplier.coupon
            减免退     root.app.supplier.order.jmt
            变价/进货  root.app.supplier.product.price_and_purchase
            预售商品   root.app.supplier.product.presell
            现货商品   root.app.supplier.product.selling
            销售订单   root.app.supplier.order */

         /*变价	root.app.supplier.product.price
进货	root.app.supplier.product.purchase*/

        //return isResources("root.app.supplier.product.price_and_purchase") && "2".equals(storeStatus);
        return isResources("root.app.supplier.product.price") && "2".equals(storeStatus);
    }

    public boolean hasPriceAuth() {
        return isResources("root.app.supplier.product.price") && "2".equals(storeStatus);
    }


    public boolean hasOfferAuth() {
        return isResources("root.app.supplier.product.often_order") && "2".equals(storeStatus);
    }


    public boolean hasStockAuth() {
        return isResources("root.app.supplier.product.purchase") && "2".equals(storeStatus);
    }

    @Override
    public String toString() {
        return JsonUtils.toJson(this);
    }


    public List<AuthStatus> getAuthStatus() {
        List<AuthStatus> list = new ArrayList<>();

        /*
         *
         *
         * */

         /* 我要买    root.app.supplier.buy
            店铺装修    root.app.supplier.store.decoration
            店铺管理    root.app.supplier.store
            消息推送    root.app.supplier.push_message
            员工管理    root.app.supplier.employee
            客户管理    root.app.supplier.customer
            我的收入    root.app.supplier.income
            我的台帐    root.app.supplier.standing_book
            评价       root.app.supplier.comment
            优惠券     root.app.supplier.coupon
            减免退     root.app.supplier.order.jmt
            变价/进货  root.app.supplier.product.price_and_purchase
            预售商品   root.app.supplier.product.presell
            现货商品   root.app.supplier.product.selling
            销售订单   root.app.supplier.order */


        //销售订单
        updateAuth("root.app.supplier.order", list, isOwned());

        //现货商品
        updateAuth("root.app.supplier.product.selling", list, isOwned());

        //预售商品
        updateAuth("root.app.supplier.product.presell", list, isOwned());

        //进货
        updateAuth("root.app.supplier.product.purchase", list, isOwned());

        //联营报价
        updateAuth("root.app.supplier.joint.product", list, isOwned());

        //我的台帐
        updateAuth("root.app.supplier.standing_book", list, isOwned());

        //我的收入
        updateAuth("root.app.supplier.income", list, isOwned());

        //店铺管理
        updateAuth("root.app.supplier.store", list, isOwned());

        //客户管理
        updateAuth("root.app.supplier.customer", list, isOwned());

        //店铺装修
        //updateAuth("root.app.supplier.store.decoration", list, isOwned());

        //员工管理
        updateAuth("root.app.supplier.employee", list, isOwned());

        //减免退
        updateAuth("root.app.supplier.order.jmt", list, isOwned());

        //评价
        updateAuth("root.app.supplier.comment", list, isOwned());

        //优惠券
        updateAuth("root.app.supplier.coupon", list, isOwned());
        //我要买
        updateAuth("root.app.supplier.buy", list, isOwned());
        //我要卖
        updateAuth("root.app.supplier.sell", list, isOwned());

        return list;
    }


    public void updateAuth(String key, List<AuthStatus> list, boolean isAdd) {
        boolean auth = isResources(key);
        if (!auth && !isAdd) {
            return;
        }
        //0,未开,1,开店,2,过,3,未过,4,冻结
        if ("0".equals(storeStatus)
                || "1".equals(storeStatus)
                || "4".equals(storeStatus)
                || "3".equals(storeStatus)
                ) {
            if (!"root.app.supplier.store".equals(key)
                    || "4".equals(storeStatus)) {
                auth = false;
            }
        }
        if ("root.app.supplier.product.purchase".equals(key) && isAuthNeedBind()) {
            auth = false;
        }
        Log.e("disabledResources", "disabledResources:" + disabledResources);
        if (isAdd && disabledResources != null && disabledResources.size() > 0) {

            int size = disabledResources.size();
            for (int i = 0; i < size; i++) {
                String s = disabledResources.get(i);
                if (key.equals(s)) {
                    return;
                }
            }
        }
        AuthStatus authStatus = new AuthStatus();
        authStatus.setKey(key);
        authStatus.setAuth(auth);
        list.add(authStatus);

    }

    public static List<AuthStatus> getAuthStatusAll() {
       /* List<AuthStatus> list = new ArrayList<>();

        //销售订单
        updateAuth("root.app.supplier.order", list);

        //现货商品
        updateAuth("root.app.supplier.product.selling", list);

        //预售商品
        updateAuth("root.app.supplier.product.presell", list);

        //我的台帐
        updateAuth("root.app.supplier.standing_book", list);

        //我的收入
        updateAuth("root.app.supplier.income", list);

        //店铺管理
        updateAuth("root.app.supplier.store", list);

        //客户管理
        updateAuth("root.app.supplier.customer", list);

        //店铺装修
        updateAuth("root.app.supplier.store.decoration", list);

        //员工管理
        updateAuth("root.app.supplier.employee", list);

        //减免退
        updateAuth("root.app.supplier.order.jmt", list);

        //评价
        updateAuth("root.app.supplier.comment", list);

        //优惠券
        updateAuth("root.app.supplier.coupon", list);

        return list;*/

        return getAuthStatusAll(null);
    }

    public static List<AuthStatus> getAuthStatusAll(ArrayList<String> disabledResources) {
        List<AuthStatus> list = new ArrayList<>();

        //销售订单
        updateAuth("root.app.supplier.order", list, disabledResources);

        //现货商品
        updateAuth("root.app.supplier.product.selling", list, disabledResources);

        //预售商品
        updateAuth("root.app.supplier.product.presell", list, disabledResources);

        //进货
        updateAuth("root.app.supplier.product.purchase", list, disabledResources);

        //联营报价
        //updateAuth("root.app.supplier.joint.product", list, disabledResources);

        //我的台帐
        updateAuth("root.app.supplier.standing_book", list, disabledResources);

        //我的收入
        updateAuth("root.app.supplier.income", list, disabledResources);

        //店铺管理
        updateAuth("root.app.supplier.store", list, disabledResources);

        //客户管理
        updateAuth("root.app.supplier.customer", list, disabledResources);

        //店铺装修
        //updateAuth("root.app.supplier.store.decoration", list, disabledResources);

        //员工管理
        updateAuth("root.app.supplier.employee", list, disabledResources);

        //减免退
        updateAuth("root.app.supplier.order.jmt", list, disabledResources);

        //评价
        updateAuth("root.app.supplier.comment", list, disabledResources);

        //优惠券
        updateAuth("root.app.supplier.coupon", list, disabledResources);

        //我要买
        updateAuth("root.app.supplier.buy", list, disabledResources);
        //我要卖
        updateAuth("root.app.supplier.sell", list, disabledResources);

        return list;
    }

    public static void updateAuth(String key, List<AuthStatus> list) {

        updateAuth(key, list, null);
    }

    public static void updateAuth(String key, List<AuthStatus> list, ArrayList<String> disabledResources) {

        boolean auth = true;
        if (!"root.app.supplier.store".equals(key)) {
            auth = false;
        }

        if (disabledResources != null && disabledResources.size() > 0) {

            int size = disabledResources.size();
            for (int i = 0; i < size; i++) {
                String s = disabledResources.get(i);
                if (key.equals(s)) {
                    return;
                }
            }
        }
        AuthStatus authStatus = new AuthStatus();
        authStatus.setKey(key);
        authStatus.setAuth(auth);
        list.add(authStatus);
    }

}
